package com.zmn.oms.zmn.aop;

/**
 * 类描述：订单支付修改后，ES 同步处理
 *
 * @author wangxiaokun
 * @since 2021/08/19 10:49
 */

import com.zmn.oms.model.dto.work.orderpay.CancelPayDTO;
import com.zmn.oms.model.entity.pay.OrderPay;
import com.zmn.oms.services.interfaces.datasync.ElasticSearchDataSyncService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

@Slf4j
@Aspect
@Configuration
// @ConditionalOnProperty(name = "elastic.sync.type", havingValue = "aop", matchIfMissing = true)
@Order(-2)
public class ZsOrderPayChangeSyncToEsAspect {

    @Autowired
    private ElasticSearchDataSyncService elasticSearchDataSyncService;

    /**
     * 设定切入点
     */
    @Pointcut("@annotation(com.zmn.oms.common.annotation.OrderPayChangeSync)")
    private void orderPointCut() {
    }

    @After("orderPointCut()")
    public void orderDoAfter(JoinPoint joinPoint) {

        Long orderId = null;
        if (joinPoint.getArgs() != null && joinPoint.getArgs().length > 0) {
            if (joinPoint.getArgs()[0] instanceof OrderPay) {
                OrderPay orderPay = (OrderPay) joinPoint.getArgs()[0];
                orderId = orderPay.getOrderId();
            } else if (joinPoint.getArgs()[0] instanceof CancelPayDTO) {
                CancelPayDTO cancelPayDTO = (CancelPayDTO) joinPoint.getArgs()[0];
                orderId = cancelPayDTO.getOrderId();
            }
        }

        if (orderId != null) {
            log.info("aop#elastic同步--->orderId:{}", orderId);
            // es同步支付明细
            elasticSearchDataSyncService.syncPayIndex(orderId);
        }
    }
}
